<template>
  <div class="login">
    <div class="wz_box">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            class="emil"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            type="password"
          ></el-input>
        </el-form-item>
        <div class="from-btn">
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >登录</el-button
            >
            <el-button type="info" @click="resetForm('ruleForm')"
              >重置</el-button
            >
          </el-form-item>
        </div>
      </el-form>
      <img src="../assets/logo.png" alt="" class="images" />
    </div>
  </div>
</template>
<script>
import { pleaLogin } from "../utils/http";
export default {
  name:'login',
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入登录名称", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //   alert("submit!");
          pleaLogin(this.ruleForm).then((res) => {
            console.log(res);
            if (res.meta.status == 200) {
              localStorage.setItem("token", res.data.token);
              this.$router.push("/welcome");
               this.$message({
                message: "登录成功",
                type: "success",
              });
            } else {
              this.$message({
                message: "密码或用户名错误",
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  height: 100vh;
  background-color:#2b4b6b ;
  .wz_box {
    width: 520px;
    height: 300px;
    background-color: #fff;
    border-radius: 5px;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 18px;
    padding-top: 100px;
    box-shadow: 0px 0px 20px #fff;
  }
  .images {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    border: 8px solid #fff;
    box-shadow: 0px 0px 20px #ddd;
    // padding: 20px;
    background-color: #eeeeee;
    position: absolute;
    left: 50%;
    top: -50px;
    transform: translate(-50%, 0%);
  }
  .from-btn {
    text-align: right;
  }
}
</style>